package com.menhai.personmanagement.plus;

import org.apache.commons.codec.binary.Base64;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStream;
import java.math.BigDecimal;
import java.net.URL;
import java.net.URLConnection;

/**
 * 图片的工具类
 *
 * @author shikun
 */
public class FileUtils {


    /**
     * 获取网络图片
     *
     * @param url
     * @throws Exception
     */
    public static InputStream getImgStream(String url) throws Exception {
        URL httpurl = new URL(url);
        URLConnection con = httpurl.openConnection();
        //不超时
        con.setConnectTimeout(0);
        //不允许缓存
        con.setUseCaches(false);
        con.setDefaultUseCaches(false);
        InputStream is = con.getInputStream();
        //先读入内存
        ByteArrayOutputStream buf = new ByteArrayOutputStream(8192);
        byte[] b = new byte[1024];
        int len;
        while ((len = is.read(b)) != -1) {
            buf.write(b, 0, len);
        }
        //读图像
        is = new ByteArrayInputStream(buf.toByteArray());
        return is;
    }


    /**
     * 获取网络图片
     *
     * @param url
     * @throws Exception
     */
    public static BufferedImage getImg(String url) throws Exception {
        URL httpurl = new URL(url);
        URLConnection con = httpurl.openConnection();
        //不超时
        con.setConnectTimeout(0);
        //不允许缓存
        con.setUseCaches(false);
        con.setDefaultUseCaches(false);
        InputStream is = con.getInputStream();
        //先读入内存
        ByteArrayOutputStream buf = new ByteArrayOutputStream(8192);
        byte[] b = new byte[1024];
        int len;
        while ((len = is.read(b)) != -1) {
            buf.write(b, 0, len);
        }
        //读图像
        is = new ByteArrayInputStream(buf.toByteArray());
        return ImageIO.read(is);
    }

    /**
     * 保存图片到指定目录下
     *
     * @param savePath
     * @param imgName
     * @param image
     * @throws Exception
     */
    public static void saveImg(String savePath, String imgName, BufferedImage image) throws Exception {
        //创建图片文件
        File uploadedImg = new File(savePath, imgName);
        //检查目录是否存在
        File dir = uploadedImg.getParentFile();
        if (!dir.exists()) {
            dir.mkdirs();
        }
        uploadedImg.createNewFile();
        //写入图片
        ImageIO.write(image, getFileType(imgName), uploadedImg);
    }

    /**
     * 获取文件后缀
     *
     * @param fileName
     * @return
     */
    public static String getFileType(String fileName) {
        if (fileName != null && fileName.contains(".")) {
            return fileName.substring(fileName.lastIndexOf("."));
        }
        return "";
    }

    /**
     * base码转流
     *
     * @param base64String
     * @return
     */
    public static InputStream getStringImage(String base64String) {
        return new ByteArrayInputStream(Base64.decodeBase64(base64String));
    }

    /**
     * 获取文件大小
     * @param size 文件大小
     * @return string
     */
    public static String getFileSize(long size) {
        // 如果字节数少于1024，则直接以B为单位，否则先除于1024，后3位因太少无意义
        double value = (double) size;
        if (value < 1024) {
            return value + "B";
        } else {
            value = new BigDecimal(value / 1024).setScale(2, BigDecimal.ROUND_DOWN).doubleValue();
        }
        // 如果原字节数除于1024之后，少于1024，则可以直接以KB作为单位
        // 因为还没有到达要使用另一个单位的时候
        // 接下去以此类推
        if (value < 1024) {
            return value + "KB";
        } else {
            value = new BigDecimal(value / 1024).setScale(2, BigDecimal.ROUND_DOWN).doubleValue();
        }
        if (value < 1024) {
            return value + "MB";
        } else {
            // 否则如果要以GB为单位的，先除于1024再作同样的处理
            value = new BigDecimal(value / 1024).setScale(2, BigDecimal.ROUND_DOWN).doubleValue();
            return value + "GB";
        }
    }
}